package cn.lzh.controller;

import cn.lzh.entity.*;
import cn.lzh.service.*;
import cn.lzh.tool.RespJson;
import cn.lzh.tool.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin//允许跨域
@RequestMapping("/api/public")
@RestController
public class PublicController {
    @Autowired
    UserService userService;
    @Autowired
    BooktypeService booktypeService;
    @Autowired
    BookService bookService;
    @Autowired
    LargefileService largefileService;
    @Autowired
    HotService hotService;
    @Autowired
    CartService cartService;


    @RequestMapping("/findHot/{num}/{type}")
    public Map<String,Object> findHot(@PathVariable("num") int num, @PathVariable("type") int type){
//        select *  from t_hot  where type=#{type} order by hot limit 0, #{num}
        List<Hot> lst = hotService.findByType(type , num );
        return RespJson.pack(200,"获取成功",lst);
    }

    @RequestMapping("/book/findIf/{start}/{num}")
    public Map<String, Object> findBookIf(@PathVariable("start") Integer start, @PathVariable("num") Integer num, String key) {
        System.out.println("start=" + start + "  num=" + num + "  key=" + key);
        if (key == null || key.length() < 2)
            return RespJson.pack(100, "搜索关键字不能短于 2  ", null);
        List<Book> lst = bookService.findByName(key, start, num);
        //  查询总数量
        int total = bookService.getCountByName(key);
        Map<String, Object> map = new HashMap<>();
        map.put("books", lst);
        map.put("total", total);
        return RespJson.pack(200, "", map);
    }


    //添加用户
    @RequestMapping("/addUser")
    public Map<String, Object> register(@RequestBody MyUser user) {
        user.setRole("P");
        userService.add(user);
        return RespJson.pack(200, "注册成功...", null);
    }


    //分页
    @RequestMapping("/findBookByPage/{start}/{num}/{typeid}")
    public Map<String, Object> findPagesbook(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid) {
        Map<String, Object> map = new HashMap<>();
        List<Book> lst =bookService.findPage(start*num, num, typeid);
        map.put("books", lst);
        int count = bookService.getCount(typeid);
        map.put("total", count);
        return RespJson.pack(200, "", map);
    }


//显示图片
    @RequestMapping("/showImg/{id}")
    public void dispaly(@PathVariable("id") String id, HttpServletResponse response) {
        byte[] buf;
        if (id == null)
            return;
        Largefile largeFile = largefileService.findById(id);
        try {
            buf = (byte[])largeFile.getContent();
        } catch (NullPointerException e) {
            buf = new byte[1];
        }
        try {
            response.setContentType("image/jpeg");
            response.getOutputStream().write(buf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //查询书本类型
    @RequestMapping("/type/findAll")
    public Map<String, Object> getBookTypes() {
        List<BookType> types = booktypeService.findAll();
        return RespJson.pack(200, "", types);
    }



    //获得点销量 5 的商品
    @RequestMapping("/book/findSale5")
    public Map<String,Object> findSale5(){
        List<Book> lst = bookService.findSale5();
        return RespJson.pack(200,"",lst);
    }

    // 获得点加到购物车前 5 的商品
    @RequestMapping("/book/findFav5")
    public Map<String,Object> findFav5(){
        List<Book> lst = bookService.findFav5();
        return RespJson.pack(200,"",lst);
    }


    //获得点赞前 5 的商品
    @ResponseBody
    @RequestMapping("/book/findRate5")
    public Map<String, Object> findRate5(){
        List<Book> lst = bookService.findRate5();
        return RespJson.pack(200, "获得点赞前 5 的商品",lst);
    }


    @RequestMapping("/findBookByPageRate/{start}/{num}/{typeid}")
    public Map<String,Object> findBookByPageRate(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid){
        Map<String, Object> map = new HashMap<>();
        List<Book> lst = bookService.findPageRate(start*num, num, typeid);
        map.put("books", lst);
        int count = bookService.getCount(typeid);
        map.put("total", count);
        return RespJson.pack(200, "", map);
    }

    //  分页查找书本信息并按照销量从高到低排序
    @RequestMapping("/findBookByPageSale/{start}/{num}/{typeid}")
    public Map<String,Object> findBookByPageSale(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid){
        Map<String, Object> map = new HashMap<>();
        List<Book> lst =bookService.findPageSale(start*num, num, typeid);
        map.put("books", lst);
        int count = bookService.getCount(typeid);
        map.put("total",count);
        return RespJson.pack(200, "", map);
    }


    //  分页查找书本信息并按照购物车从高到低
    @RequestMapping("/findBookByPageFav/{start}/{num}/{typeid}")
    public Map<String,Object> findBookByPageFav(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid){
        Map<String, Object> map = new HashMap<>();
        List<Book> lst = bookService.findPageFav(start*num, num, typeid);
        map.put("books", lst);
        int count = bookService.getCount(typeid);
        map.put("total", count);
        return RespJson.pack(200, "", map);
    }


}
